import { Controller, Get, Post, Body, Patch, Param, Delete, UseInterceptors, UploadedFile, BadRequestException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery } from "@nestjs/swagger";
import { NovelService } from './novel.service';
import { BookDto } from './dto/book.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { Public } from '@/common/decorators';


@ApiTags("书籍管理")
@Controller('novel')
export class NovelController {
  constructor(private readonly novelService: NovelService) { }

  @Public()
  @ApiOperation({ summary: '保存、修改书籍' })
  @Post('save')
  @UseInterceptors(FileInterceptor('file'))
  async saveNovel(@UploadedFile() file: Express.Multer.File, @Body() bookDto: BookDto) {
    if (bookDto.id) {
      return this.novelService.updateBook(bookDto.id, bookDto, file);
    } else {
      return this.novelService.createBook(bookDto, file);
    }
  }

  @Public()
  @ApiOperation({ summary: "上传书籍" })
  @Post('upload')
  @UseInterceptors(FileInterceptor('file'))
  async uploadNovel(@UploadedFile() file: Express.Multer.File) {
    if (!file) {
      throw new BadRequestException("文件不能为空")
    }
    return this.novelService.upload(file);
  }

}
